//
//  RLTabBar.m
//  RelaxLinkProduct
//
//  Created by relax on 2017/11/20.
//  Copyright © 2017年 relax. All rights reserved.
//

#import "RLTabBar.h"
#import <objc/runtime.h>


@implementation RLTabBar {
    /** 最后一个选中的按钮 */
    UIButton *_lastButton;
    /** 开启直播的 button */
    UIButton *_cameraButton;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initUI];
    }
    
    return self;
}

/** 初始化 UI */
- (void)initUI {
    /** 设置背景图片 */
    UIImage *image = [UIImage imageNamed:@"global_tab_bg_768x49_"];
    UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:image];
    backgroundImageView.frame = self.bounds;
    [self addSubview:backgroundImageView];
    // self.backgroundColor = [UIColor orangeColor];
    
    /** 设置按钮 */
    [self prepareButtons];
    
    /** 直播按钮 */
    _cameraButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_cameraButton setImage:[UIImage imageNamed:@"tab_launch"] forState:UIControlStateNormal];
    [_cameraButton sizeToFit];
    _cameraButton.rl_centerX = self.rl_centerX;
    _cameraButton.rl_centerY = 0;
    _cameraButton.tag = TabBarButtonItemCamera;
    [_cameraButton addTarget:self action:@selector(tabButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_cameraButton];
}

- (void)prepareButtons {
    
    NSArray *buttons = @[@"tab_me",@"tab_live"];
    /** 
     tab_me
     tab_launch
     tab_live
     */
    
    for (NSInteger i = 0; i < buttons.count; i++) {
        UIButton *tabButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [tabButton setImage:[UIImage imageNamed:buttons[i]] forState:UIControlStateNormal];
        [tabButton setImage:[UIImage imageNamed:[buttons[i] stringByAppendingString:@"_p"]] forState:UIControlStateSelected];
        [tabButton sizeToFit];
        tabButton.adjustsImageWhenHighlighted = NO;// 取消默认高亮效果
        tabButton.tag = 100 + i;
        
        [tabButton addTarget:self action:@selector(tabButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        if (i == 0) {
            tabButton.selected = YES;
            _lastButton = tabButton;
        }
        
        [self addSubview:tabButton];
    }
}

#pragma mark - 按钮点击
- (void)tabButtonClick:(UIButton *)button {
    if ([self.delegate respondsToSelector:@selector(tabBar:didSelectedButton:)]) {
        [self.delegate tabBar:self didSelectedButton:button.tag];
    } else {
        self.block ? self.block(self,button.tag) : nil;
    }
    
    
    if (button.tag == TabBarButtonItemCamera) {
        return;
    }
    
    _lastButton.selected = NO;
    _lastButton = [self viewWithTag:button.tag];
    _lastButton.selected = YES;
    
    [UIView animateWithDuration:0.25 animations:^{
        /** 坑真 J8多。 */
        _lastButton.imageView.transform = CGAffineTransformMakeScale(1.2, 1.2);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.25 animations:^{
            _lastButton.imageView.transform = CGAffineTransformIdentity;
        }];
    }];
}

#pragma mark - layoutSubviews
- (void)layoutSubviews {
    [super layoutSubviews];
    static NSUInteger index = 0;
    // 每一个 button 的宽度
    CGFloat w = self.bounds.size.width * 0.5;
    CGFloat h = self.bounds.size.height;
    for (NSInteger i = 0; i < self.subviews.count; i++) {
        UIView *view = self.subviews[i];
        if (view.tag == TabBarButtonItemCamera) continue;
        if ([view isKindOfClass:[UIButton class]]) {
            view.frame = CGRectMake(index * w, 0, w, h);
            index++;
        }
    }
}



#pragma mark - hitTest
//- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
//    NSLog(@"%@",@"进不来吗?");
//    return [super hitTest:point withEvent:event];
//}

@end
